package com.pro.addresslist.ui.common;

import android.app.Application;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager.BadTokenException;
import android.widget.ImageView;
import android.widget.TextView;

import com.pro.addresslist.R;
/**
 * 
 * @author Gordon.li
 *
 */
public abstract class BaseFragment extends Fragment implements OnClickListener {
	private ProgressDialog progressDialog;
	public View mView;
	public Context mContext;
	private boolean loadTitle = false;
	// Title的控件
	private ImageView mIvTitleLeft, mIvTitleRight;
	private TextView mTvTitleRight, mTvTitleCenter;
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		OnViewClick(v);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (getLayout() != 0) {
			mView = inflater.inflate(getLayout(), null);

			mContext = getActivity();
		}
		if (mView != null) {

			if ((mView.findViewById(R.id.rl_title_layout)) != null) {
				loadTitle = true;
				loadTitle();
			}
		}

		OnActCreate(inflater, container, savedInstanceState);
		return mView;
	}

	/**
	 * 加载标题
	 */
	private void loadTitle() {
		mIvTitleLeft = (ImageView) mView.findViewById(R.id.iv_title_left_image);
		mIvTitleRight = (ImageView) mView
				.findViewById(R.id.iv_title_right_image);
		mTvTitleCenter = (TextView) mView
				.findViewById(R.id.tv_title_center_text);
		mTvTitleRight = (TextView) mView.findViewById(R.id.tv_title_right_text);
	}

	// 设置左边图片 自定义点击事件
	public void setLeftIamge(int resId, OnClickListener listener) {
		if (loadTitle) {
			mIvTitleLeft.setVisibility(View.VISIBLE);
			mIvTitleLeft.setImageResource(resId);
			mIvTitleLeft.setOnClickListener(listener);

		}
	}

	// 设置左边图片 默认返回
	public void setLeftImageBack() {
		if (loadTitle) {
			mIvTitleLeft.setVisibility(View.VISIBLE);
			mIvTitleLeft.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					getActivity().finish();
				}
			});
		}

	}

	 //设置左边字体 自定义点击事件
//		public void setLeftText(String text) {
//			if (loadTitle) {
//				mTvTitleLeft.setVisibility(View.VISIBLE);
//				mTvTitleLeft.setText(text);
//			}
//		}

	// 设置标题
	public void setTitle(String text) {
		if (loadTitle) {
			mTvTitleCenter.setVisibility(View.VISIBLE);
			mTvTitleCenter.setText(text);
		}
	}

	// 设置右边图片 自定义点击事件
	public void setRightImage(int resid, OnClickListener listener) {
		if (loadTitle) {
			mIvTitleRight.setVisibility(View.VISIBLE);
			mIvTitleRight.setImageResource(resid);
			mIvTitleRight.setOnClickListener(listener);
		}
	}

	// 设置右边字体 自定义点击事件
	public void setRightText(String text) {
		if (loadTitle) {
			mTvTitleRight.setVisibility(View.VISIBLE);
			mTvTitleRight.setText(text);
		}
	}

	/**
	 * 给控件设置监听
	 * 
	 * @param resId
	 * @param listener
	 */
	public View setViewClick(int resId) {
		View view = mView.findViewById(resId);
		if (view != null) {
			view.setOnClickListener(this);
		}
		return view;
	}
	
	/**
	 * 显示短时间的提示
	 * 
	 * @param info
	 */
	public void ShowToast(String info) {
		MyApplication.getInstance().showToast(info);
	}

	/**
	 * 显示正在加载的进度条
	 * 
	 */
	public void showProgressDialog() {
		showProgressDialog("努力加载中...");
	}

	public void showProgressDialog(String msg) {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
			progressDialog = null;
		}
		progressDialog = new ProgressDialog(getActivity());
		progressDialog.setMessage(msg);
		progressDialog.setCanceledOnTouchOutside(false);
		progressDialog.setCancelable(true);
		try {
			progressDialog.show();
		} catch (BadTokenException exception) {
			exception.printStackTrace();
		}
	}

	public void showProgressingDialog() {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
			progressDialog = null;
		}
		progressDialog = new ProgressDialog(getActivity());
		progressDialog.setMessage("努力加载中...");
		progressDialog.setCanceledOnTouchOutside(false);
		progressDialog.setCancelable(true);
		try {
			progressDialog.show();
		} catch (BadTokenException exception) {
			exception.printStackTrace();
		}
	}
	/**
	 * 取消对话框显示
	 */
	public void disMissDialog() {
		try {
			if (progressDialog != null && progressDialog.isShowing()) {
				progressDialog.dismiss();
				progressDialog = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取界面要加载的布局文件
	 * 
	 * @return
	 */
	public abstract int getLayout();

	/**
	 * 子类的OnCreate()方法
	 * 
	 * @param savedInstanceState
	 */
	public abstract void OnActCreate(LayoutInflater inflater,
			ViewGroup container, Bundle savedInstanceState);

	/**
	 * 控件的点击事件
	 * 
	 * @param v
	 */
	public abstract void OnViewClick(View v);



	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		disMissDialog();
	}
}
